package com.guanghua.brick.html;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TabControl extends WebControl {

	//private static Log logger = LogFactory.getLog(SelectControl.class);

	private String textProperty = "text";
	private String valueProperty = "value";
	//控件装载的tab页的html内容
	private String body = null;
	//默认的值
	private String defaultData = "1";
	//比较方式
	private boolean selectedByIndex = true;
	
	@Override
	public String buildHTML(HttpServletRequest request, HttpServletResponse response) {
		StringBuffer html = new StringBuffer();
		html.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" ").append(this.getHtml()).append(">");
		html.append("<tr valign=\"bottom\">\n");
		
		
		//拼装tab
		int size = 1;
		if (this.getContent() != null) {
			//去content
			List<Map<String, String>> list = this.getContent().buildContent(request, response);
			String d = defaultData;
			if (this.getData() != null && this.getData().length() != 0) d = this.getData();
			
			int index = 1;
			for (Map<String, String> map : list) {
				String text = map.get(this.textProperty);
				String value = map.get(this.valueProperty);
				
				//value是tab页的名字，text是url
				if (text == null || value == null) continue;
				
				
				//判断是否是在当前tab页中
				if (isSeleceted(value, index++, d)) {
					html.append("	<td class=\"tab_selected\">")
						.append(value)
						.append("</td>\n");
				} else {
					html.append("	<td class=\"tab\"><a href=\"")
					.append(text)
					.append("\">")
					.append(value)
					.append("</a></td>\n");
				}
			}
			size += list.size();
		}
		
		html.append("	<td>&nbsp;</td>\n");
		html.append("</tr>\n");
		html.append("<tr><td class=\"tab_underline\" colspan=\"").append(size).append("\"></td></tr>\n");
		
		//如果该控件是包括内容的
		if (this.body != null)
			html.append("<tr><td class=\"tab_table\" colspan=\"")
				.append(size)
				.append("\">\n")
				.append(this.body)
				.append("\n</td></tr>\n");
		
		html.append("</table>\n");
		return html.toString();
	}
	
	private boolean isSeleceted(String value, int index, String data) {
		if (this.isSelectedByIndex()) {
			return String.valueOf(index).equals(data);
		} else {
			return value.equals(data);
		}
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getTextProperty() {
		return textProperty;
	}

	public void setTextProperty(String textProperty) {
		this.textProperty = textProperty;
	}

	public String getValueProperty() {
		return valueProperty;
	}

	public void setValueProperty(String valueProperty) {
		this.valueProperty = valueProperty;
	}

	public String getDefault() {
		return defaultData;
	}

	public void setDefault(String defaultData) {
		this.defaultData = defaultData;
	}

	public boolean isSelectedByIndex() {
		return selectedByIndex;
	}

	public void setSelectedByIndex(boolean selectedByIndex) {
		this.selectedByIndex = selectedByIndex;
	}

	
	
}
